<?php
/**
 *   OPENSHOP 站外JS投放的统计程序
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: adsense.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 站外投放广告的统计
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list' || $_REQUEST['act'] == 'download')
{
    admin_priv('veiw_ads_js_flow');

    /* 获取广告数据 */
    $ads_stats = array();
    $sql = "SELECT a.ad_id, a.ad_name, b.* ".
           "FROM " .$os->table('ad'). " AS a, " .$os->table('adsense'). " AS b ".
           "WHERE b.from_ad = a.ad_id ORDER by a.ad_name DESC";
    $res = $db->query($sql);
    while ($rows = $db->fetchRow($res))
    {
        /* 获取当前广告所产生的订单总数 */
        $sql2 = 'SELECT COUNT(order_id) FROM ' .$os->table('order_info'). " WHERE from_ad='$rows[ad_id]' AND referer='$rows[referer]'";
        $rows['order_num'] = $db->getOne($sql2);

        /* 当前广告所产生的已完成的有效订单 */
        $sql3 = "SELECT COUNT(order_id) FROM " .$os->table('order_info').
               " WHERE from_ad    = '$rows[ad_id]'" .
               " AND referer = '$rows[referer]' ". order_query_sql('finished');
        $rows['order_confirm'] = $db->getOne($sql3);

        $ads_stats[] = $rows;
    }
    $smarty->assign('ads_stats',        $ads_stats);

    /* 站外JS投放商品的统计数据 */
    $goods_stats    = array();
    $goods_sql      = "SELECT from_ad, referer, clicks FROM " .$os->table('adsense').
              " WHERE from_ad = '-1' ORDER by referer DESC";
    $goods_res = $db->query($goods_sql);
    while ($rows2 = $db->fetchRow($goods_res))
    {
        /* 获取当前广告所产生的订单总数 */
        $rows2['order_num'] = $db->getOne("SELECT COUNT(order_id) FROM " .$os->table('order_info'). " WHERE referer='$rows2[referer]'");

        /* 当前广告所产生的已完成的有效订单 */
        $sql = "SELECT COUNT(order_id) FROM " .$os->table('order_info').
               " WHERE referer='$rows2[referer]'" . order_query_sql('finished');
        $rows2['order_confirm'] = $db->getOne($sql);

        $rows2['ad_name']  = '站外JS调用商品';
        $goods_stats[]  = $rows2;
    }
    if ($_REQUEST['act'] == 'download')
    {
        admin_priv('down_ads_js_flow');
        
        header("Content-type: application/vnd.ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename=ad_statistics.xls");
        $data = "投放的名称\t点击来源\t点击次数\t有效订单数\t产生订单总数\n";
        $res = array_merge($goods_stats, $ads_stats);
        foreach ($res AS $row)
        {
            $data .= "$row[ad_name]\t$row[referer]\t$row[clicks]\t$row[order_confirm]\t$row[order_num]\n";
        }
        echo os_iconv(OS_CHARSET, 'GB2312', $data);
        exit;
    }
    $smarty->assign('goods_stats', $goods_stats);

    /* 赋值给模板 */
    $smarty->assign('action_link', array('href' => 'ads.php?act=list', 'text' => '广告列表'));
    $smarty->assign('action_link2', array('href' => 'adsense.php?act=download', 'text' => '下载广告统计报表'));
    $smarty->assign('ur_here',     '站外投放JS统计');

    /* 显示页面 */
    assign_query_info();
    
    $smarty->display("adsense.tpl");   
}
?>